﻿/// <reference path="utility.js" />
var info = '<div class="msg-warn"><b></b>公共场所不建议记住密码，以防账号丢失</div>';
var info2 = '<div class="msg-error"><b></b>请输入用户名和密码</div>';

$(function () {
    $('.msg-wrap .msg-error').hide();
    //$('.msg-wrap .msg-warn').replaceWith(info);
    $("#btnLogins").click(function () {
        if (checkform()) {

            PostLogin();
        } else {
            //$('.msg-wrap .msg-warn').hide();
            $('.msg-wrap').show();
            $('.msg-wrap').replaceWith(info2);

            return;
        }
    });

    $(document).keyup(function (event) {
        if (event.keyCode == 13) {
            $("#btnLogins").click();
        }
    });
});

function PostLogin() {
    $(".login_p").hide();
    $(".login_s").show();
    //$("#ctl00_ContentPlaceHolder1_btnLogin").click();
    var d = {};
    $("input:hidden").each(function (i, n) {
        eval("d." + $(n).attr("name") + " = $(n).val()");
    });
    $(".itxt").each(function (i, n) {
        eval("d." + $(n).attr("name") + " = $(n).val()");
    });
    d.ctl00$ContentPlaceHolder1$btnLogin = "登陆";

    checkCount = 0;
    $.post($("#aspnetForm").attr("action"), d, function (data) {
        if (data.IsSuccess) {
            //登陆测评系统
            CheckLogin(data);

        } else {
            $(".login_s").hide();
            $(".login_p").show();
            alert(data.Message);
        }

    }, "json").error(function () {
        $(".login_s").hide();
        $(".login_p").show();
    });
}

var checkCount = 0;

function CheckLogin(suData) {
    $.get($("#aspnetForm").attr("action"), {
        CheckLogin: 1,
        r: (new Date()).getTime()
    },
        function (data) {
            if (data != "1") {
                checkCount += 1;
                alert("登录失败，请联系管理员。");
                if (checkCount > 5) {
                    // PostLogin();
                    alert("登录失败，请联系管理员。");
                } else {
                    CheckLogin(suData);
                }
            } else {
                // alert(data.ExamUrl);
                $("#ifExam").on("load", function () {
                    //加载完测评系统
                    window.location.href = suData.BackUrl;
                });
                $("#ifExam").attr("src", suData.ExamUrl);
                //5秒还没跳转，强制跳转
                setTimeout("gotoUrl('" + suData.BackUrl + "')", 5000);
            }
        });
}

function gotoUrl(url) {
    window.location.href = url;
}
function querystring(key) {
    var re = new RegExp('(?:\\?|&)' + key + '=(.*?)(?=&|$)', 'gi');
    var r = [], m;
    while ((m = re.exec(document.location.search)) != null) r.push(m[1]);
    return r.toString();
}
//验证
function checkform() {
    var namePlace = $("#ctl00_ContentPlaceHolder1_txtLoginName").attr("placeholder");
    var pdwPlace = $("#ctl00_ContentPlaceHolder1_txtLoginPwd").attr("placeholder");;
    var rs = true;
    var name = $("#ctl00_ContentPlaceHolder1_txtLoginName").val().replace(namePlace, "");
    var pwd = $("#ctl00_ContentPlaceHolder1_txtLoginPwd").val().replace(pdwPlace, "");;
    if (!Utils.ValidateRequired(name)) {
        rs = false;

    }
    if (!Utils.ValidateRequired(pwd)) {
        rs = false;
    }

    return rs;
}
